package com.itic.web.index.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.actcms.bannerarticle.persistence.model.BannerArticle;
import com.itic.ht.actcms.bannerarticle.persistence.model.BannerArticleExample;
import com.itic.ht.actcms.bannerarticle.service.BannerArticleService;
import com.itic.ht.actcms.bannertopic.persistence.model.BannerTopic;
import com.itic.ht.actcms.bannertopic.persistence.model.BannerTopicExample;
import com.itic.ht.actcms.bannertopic.service.BannerTopicService;
import com.itic.ht.actcms.globaltype.persistence.model.GlobalType;
import com.itic.ht.actcms.globaltype.persistence.model.GlobalTypeExample;
import com.itic.ht.actcms.globaltype.service.GlobalTypeService;
import com.itic.ht.actcms.knowledge.persistence.model.WebKnowledge;
import com.itic.ht.actcms.knowledge.persistence.model.WebKnowledgeExample;
import com.itic.ht.actcms.knowledge.service.WebKnowledgeService;
import com.itic.ht.file.webfile.persistence.model.WebFile;
import com.itic.ht.file.webfile.service.WebFileService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.DataResult;
import com.itic.web.ro.Datas;
import com.itic.web.ro.Datav;
import com.itic.web.ro.RoResult;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class AppIndexService {
	

	@Autowired
	private WebMemberService webMemberService;
    @Autowired
    private WebFileService webFileService;
    @Autowired
    private BannerTopicService bannerService;
    @Autowired
    private BannerArticleService bannerArticleService;
    @Autowired
    private GlobalTypeService globalTypeService;
    @Autowired
    private WebKnowledgeService knowledgeService;
    @Autowired
    private ModelFilesService modelFilesService;

	
	public DataResult getIndex(TokenData data){
		
		WebMember member = null;
		Datas datas = new Datas();
		List<BannerTopic> bannerList = null;
		List<GlobalType> globalList = null;
		List<WebKnowledge> knowledgeList = null;
		try {
			if(StringHelper.isEmpty(data.getToken())){
				return DataResult.error(1, "token不能为空");
			}
			//先判断
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return DataResult.error(2, "token失效");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			System.out.println("现在要查询的帐号是:"+data.getAccount());
			member = webMemberService.getMemberByAccount(data.getAccount());
			//变更图片
         	String memfileId = member.getFileId();
         	if(memfileId!=null && !"".equals(memfileId)){
         		WebFile webFile = webFileService.get(member.getFileId());
    		     if(webFile!=null){
    		    	 member.setFileId(webFile.getFilePath());
    		     }
         	}
			
			
			//查询bannerList
			BannerTopicExample bannerExample = new BannerTopicExample();
			bannerExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
			bannerList = bannerService.list(bannerExample);
			for (BannerTopic banner : bannerList) {
             	String bannerfileId = banner.getFileId();
             	if(bannerfileId!=null && !"".equals(bannerfileId)){
             		banner.setFileId(modelFilesService.getFile(bannerfileId).getFilePath());
             	}
			}
			
			//查询globalList
			GlobalTypeExample globalExample = new GlobalTypeExample();
			globalExample.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
			globalList = globalTypeService.list(globalExample);
			
			//查询knowledgeList
			WebKnowledgeExample knowledgeExample = new WebKnowledgeExample();
			knowledgeExample.createCriteria().andCategoryEqualTo("001").andIsEffectiveEqualTo(DBConst.TRUE);
			knowledgeList = knowledgeService.list(knowledgeExample);
			for (WebKnowledge webKnowledge : knowledgeList) {
             	String knowledgefileId = webKnowledge.getFileId();
             	if(knowledgefileId!=null && !"".equals(knowledgefileId)){
             		webKnowledge.setFileId(modelFilesService.getFile(knowledgefileId).getFilePath());
             	}
			}
			//member,bannerList,globalList,knowledgeList
			datas.setMemdata(member);
			datas.setBannerdatas(bannerList);
			datas.setGlobaldatas(globalList);
			datas.setKnowledgedatas(knowledgeList);
			
			return DataResult.build(200, "查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			return DataResult.error(4, "查询失败");
		}
		
	}
	
	
	public RoResult getArticleList(TokenData data,String pid){
		
		WebMember member = null;
		try {
			if(StringHelper.isEmpty(data.getToken())){
				return RoResult.error(1, "token不能为空");
			}
			//先判断
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return RoResult.error(2, "token失效");
			}
			if(pid == null || "".equals(pid)){
				return RoResult.error(3, "pid为空");
			}
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			member = webMemberService.getMemberByAccount(data.getAccount());
			System.out.println("现在要查询的帐号是:"+member.getAccount());
			
			//查询banner
			BannerTopic bannerTopic = bannerService.get(pid);
			//查询bannerList
			BannerArticleExample bannerArticleExample = new BannerArticleExample();
			bannerArticleExample.createCriteria().andPidEqualTo(pid).andIsEffectiveEqualTo(DBConst.TRUE);
			List<BannerArticle> BannerArticlelist = bannerArticleService.list(bannerArticleExample);
			
			if(BannerArticlelist !=null && BannerArticlelist.size()>0){
				for (BannerArticle bannerArticle : BannerArticlelist) {
	             	String bannerArticlefileId = bannerArticle.getFileId();
	             	if(bannerArticlefileId!=null && !"".equals(bannerArticlefileId)){
	             		bannerArticle.setFileId(modelFilesService.getFile(bannerArticlefileId).getFilePath());
	             		bannerArticle.setNote(modelFilesService.getFile(bannerArticlefileId).getVedioPath());
	             	}
				}
				
			}else{
				BannerArticlelist = new ArrayList<BannerArticle>();
			}

			Datav datas = new Datav();
			datas.setData(bannerTopic);
			datas.setDatas(BannerArticlelist);
			
			
			return RoResult.build(200, "查询成功",datas);
		} catch (Exception e) {
			//返回错误代码
			return RoResult.error(4, "查询失败");
		}
		
	}
	
	
}
